import { createRouter, createWebHashHistory } from "vue-router";
import index from '../view/index.vue';
import login from '../view/Login.vue';
import register from '../view/Register.vue';
import department from "@/view/department.vue";
import employee from "@/view/employee.vue";
import attendance from "@/view/attendance.vue";
import equipment from "@/view/equipment.vue";
import inventory from "@/view/inventory.vue";
import maintenanceRecord from "@/view/maintenanceRecord.vue";
import product from "@/view/product.vue";
import productionOrder from "@/view/productionOrder.vue";
import passwordReset from "@/view/PasswordReset.vue";
import productionSchedule from "@/view/productionSchedule.vue";
// 添加采购订单导入
import purchaseOrder from "@/view/purchaseOrder.vue";
// 添加原材料管理导入
import rawMaterial from "@/view/rawMaterial.vue";
// 添加供应商管理导入
import supplier from "@/view/supplier.vue";
// 添加设备状态监控导入
import equipmentMonitoring from "@/view/equipmentMonitoring.vue";
// 添加设备维护管理流程导入
import EquipmentMaintenance from "@/view/EquipmentMaintenance.vue";
// 添加人力资源管理流程导入
import HumanResourceManagement from "@/view/HumanResourceManagement.vue";
// 添加库存管理流程导入
import InventoryManagement from "@/view/InventoryManagement.vue";
// 添加通义千问页面导入
import qwen from "@/view/qwen.vue";

const routes = [
    {
        path: '/login',
        name: 'login',
        component: login,
        beforeEnter: (to, from, next) => {
            // 如果已经登录，直接跳转到主页
            const token = localStorage.getItem('token');
            if (token) {
                next('/');
            } else {
                next();
            }
        }
    },
    {
        path: '/register',
        name: 'register',
        component: register,
        beforeEnter: (to, from, next) => {
            // 如果已经登录，直接跳转到主页（和登录页逻辑一致）
            const token = localStorage.getItem('token');
            if (token) {
                next('/');
            } else {
                next();
            }
        }
    },
    {
        path: '/password-reset',
        name: 'passwordReset',
        component: passwordReset
    },
    {
        path: '/',
        name: 'index',
        component: index,
        children: [
            { path: '', name: 'home', component: employee },
            { path: 'department', name: 'department', component: department },
            { path: 'employee', name: 'employeeList', component: employee },
            { path: 'attendance', name: 'attendance', component: attendance },
            { path: 'equipment', name: 'equipment', component: equipment },
            { path: 'inventory', name: 'inventory', component: inventory },
            { path: 'maintenanceRecord', name: 'maintenanceRecord', component: maintenanceRecord },
            { path: 'product', name: 'product', component: product },
            { path: 'productionOrder', name: 'productionOrder', component: productionOrder },
            {
                path: 'productionSchedule',
                name: 'productionSchedule',
                component: productionSchedule
            },
            // 添加采购订单路由
            { path: 'purchaseOrder', name: 'purchaseOrder', component: purchaseOrder },
            // 添加原材料管理路由
            { path: 'rawMaterial', name: 'rawMaterial', component: rawMaterial },
            // 添加供应商管理路由
            { path: 'supplier', name: 'supplier', component: supplier },
            // 添加设备维护管理流程路由
            { path: 'equipmentMaintenance', name: 'equipmentMaintenance', component: EquipmentMaintenance },
            // 添加人力资源管理流程路由
            { path: 'humanResourceManagement', name: 'humanResourceManagement', component: HumanResourceManagement },
            // 添加库存管理流程路由
            { path: 'inventoryManagement', name: 'inventoryManagement', component: InventoryManagement },
            // 添加设备状态监控路由
            { path: 'equipmentMonitoring', name: 'equipmentMonitoring', component: equipmentMonitoring },
            // 添加通义千问页面路由
            { path: 'qwen', name: 'qwen', component: qwen },
        ],
        beforeEnter: (to, from, next) => {
            // 检查是否已登录
            const token = localStorage.getItem('token');
            if (!token) {
                next('/login');
            } else {
                next();
            }
        }
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

export default router;